<template>
  <div class="app-container">
    <div class="shousuo">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-form label-width="100px">
            <el-input v-model="brandlist.question" placeholder="请输入问题"></el-input>
          </el-form>
        </el-col>
        <el-col :span="10" style="width: 57%;">
          <el-button type="primary" icon="el-icon-search" @click="setectData()">查询</el-button>
          <el-button type="primary" icon="el-icon-edit" @click="resetData()">添加</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading"
      element-loading-text="正在查询中..."
    >
      <el-table-column sortable fixed prop="id" label="问题ID" align="center" width="100"></el-table-column>
      <el-table-column prop="question" label="问题内容" align="center" width="200"></el-table-column>
      <el-table-column prop="answer" label="问题回复" align="center"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <template>
      <!-- 分页 -->
      <pagination
        :total="total"
        :page.sync="brandlist.pagenum"
        :limit.sync="brandlist.pagesize"
        @pagination="getList"
      />
      <!--添加模态框 -->
      <el-dialog
        title="添加"
        :visible.sync="addSpecDig"
        width="30%"
        :before-close="cancelAddSpecDig"
        center
      >
        <el-row>
          <el-col :span="21" :offset="1">
            <el-form
              ref="addSpecData"
              :model="addSpecData"
              label-width="100px"
              :inline="true"
              :label-position="'right'"
              status-icon
              :rules="rules"
            >
              <el-form-item prop="question" label="问题">
                <el-input v-model="addSpecData.question" />
              </el-form-item>
              <el-form-item prop="answer" label="回复" >
                <el-input v-model="addSpecData.answer" type="textarea" autosize/>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancelAddSpecDig">取 消</el-button>
          <el-button type="primary" @click="addSubmitBtn()">确定</el-button>
        </span>
      </el-dialog>
      <!--修改模态框 -->
      <el-dialog
        title="编辑"
        :visible.sync="updataSpecDig"
        width="30%"
        :before-close="cancelAddSpecDig"
      >
        <el-row>
          <el-col :span="21" :offset="1">
            <el-form
              ref="updataSpecData"
              :model="updataSpecData"
              label-width="100px"
              :inline="true"
              :label-position="'right'"
              status-icon
              :rules="rules"
            >
              <el-form-item prop="question" label="问题">
                <el-input v-model="updataSpecData.question" />
              </el-form-item>
              <el-form-item prop="answer" label="回复" >
                <el-input v-model="updataSpecData.answer" type="textarea" autosize/>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancelAddSpecDig">取 消</el-button>
          <el-button type="primary" @click="updateSubmitBtn()">确定</el-button>
        </span>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import {issuelist,issueadd,issueupdate,issuedelete } from "@/api/Shopping/Shopping";
import Pagination from "@/components/Pagination";
export default {
  components: { Pagination },
  data() {
    return {
      addSpecData: {
        question: "",
        answer: ""
      },
      updataSpecData:{
        id:"",
        question: "",
        answer: ""
      },
      addSpecDig: false,
      updataSpecDig: false,
      loading: true,
      total: 0,
      tableData: [],
      brandlist: {
        pagenum: 1,
        pagesize: 10,
        question: ""
      },
      id:{
        id:""
      },
      rules: {
        question: [{ required: true, message: "问题不能为空", trigger: "blur" }],
        answer: [{ required: true, message: "回复不能为空", trigger: "blur" }],
      }
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    getdata() {
      issuelist(this.brandlist)
        .then(res => {
          if (res.data.responseCode == 0) {
            this.total = res.data.data.total;
            this.tableData = res.data.data.list;
            this.loading = false;
          } else {
            this.$notify.error({
              title: res.data.data,
              message: res.data.responseMsg
            });
          }
        })
        .catch(response => {
          this.$notify.error({
            title: "失败",
            message: "系统出现错误"
          });
        });
    },
    //修改
    handleEdit(row) {
      this.updataSpecDig = true
      this.updataSpecData= { ...row }
    },
    //删除
    handleDelete(row) {
      this.id.id = { ...row }.id
      this.$confirm('此操作将删除该问题, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        issuedelete(this.id).then(res => {
        if (res.data.responseCode == 0) {
            this.$notify({
            title: res.data.data,
            message: res.data.responseMsg,
            type: 'success'
        });
        } else {
          this.$notify.error({
            title: "错误",
            message: res.responseMsg
          });
        }
        this.getdata()
      });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    getList(pagenum) {
      this.brandlist.pagenum = pagenum.page;
      this.brandlist.pagesize = pagenum.limit;
      this.getdata();
    },
    setectData() {
      this.getdata();
    },
    resetData() {
      this.addSpecDig = true;
    },
    cancelAddSpecDig() {
      this.updataSpecDig = false
      this.addSpecDig = false;
    },
    //修改
    updateSubmitBtn(){
      issueupdate(this.updataSpecData).then(res => {
        if (res.data.responseCode == 0) {
            this.getdata()
            this.updataSpecDig = false;
            this.$notify({
            type: 'success',
            title: res.data.data,
            message: res.data.responseMsg
          });
        } else {
          this.$notify.error({
            title: res.data.data,
            message: res.data.responseMsg
          });
        }
      });
    },
    addSubmitBtn() {
      issueadd(this.addSpecData).then(res => {
        if (res.data.responseCode == 0) {
            this.getdata()
            this.addSpecData = {}
            this.addSpecDig = false;
            this.$notify({
            type: 'success',
            title: res.data.data,
            message: res.data.responseMsg
          });
        } else {
          this.$notify.error({
            title: res.data.data,
            message: res.data.responseMsg
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.shousuo {
  float: right;
  margin-bottom: 20px;
}
</style>
